package com.tg.app.activity.device.add;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.tange.module.add.guide.OnApResetFinishListener;
import com.tg.app.R;
import com.tg.app.helper.AppHelper;

public class NormalApGuideView extends FrameLayout implements View.OnClickListener {

    public static final String TAG = "NormalApGuideView";
    private CheckBox box;
    private Button button;
    private TextView title, mark, description, help;

    private OnApResetFinishListener onApResetFinishListener;

    public void setOnApResetFinishListener(OnApResetFinishListener onApResetFinishListener) {
        this.onApResetFinishListener = onApResetFinishListener;
    }

    public NormalApGuideView(@NonNull Context context) {
        super(context);
        init();
    }

    public NormalApGuideView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public NormalApGuideView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {

        inflate(getContext(), R.layout.view_ap_guide, this);

        button = findViewById(R.id.btn_before_add_next);
        title = findViewById(R.id.tv_device_ap_add_title);
        mark = findViewById(R.id.tv_device_wifi_add_title);
        description = findViewById(R.id.tv_preparation_before_add_description);
        help = findViewById(R.id.tv_device_ap_add_help);
        help.setOnClickListener(this);
        button.setOnClickListener(this);
        button.setAlpha(0.4f);
        box = findViewById(R.id.cb_listener);
        box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    button.setAlpha(1f);
                } else {
                    button.setAlpha(0.4f);
                }
            }
        });

        findViewById(R.id.back_toolbar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getContext() instanceof Activity) {
                    Activity activity = (Activity) getContext();
                    activity.finish();
                }
            }
        });

        if (AppHelper.isDuomigeApp()){
            AppHelper.setDuoigeText(mark, box, help);
        }
    }

    public void showResetDeviceType() {
        help.setVisibility(View.GONE);
        description.setVisibility(View.GONE);
        title.setText(R.string.device_reset);

        if (onApResetFinishListener != null) {
            onApResetFinishListener.onHowToResetClick();
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.btn_before_add_next) {
            if (box.isChecked()) {
                if (onApResetFinishListener != null) {
                    onApResetFinishListener.onFinishSetting();
                }
            }
        } else if (id == R.id.tv_device_ap_add_help) {
            showResetDeviceType();
        }
    }

    protected void initView() {
        button = findViewById(R.id.btn_before_add_next);
        title = findViewById(R.id.tv_device_ap_add_title);
        mark = findViewById(R.id.tv_device_wifi_add_title);
        description = findViewById(R.id.tv_preparation_before_add_description);
        help = findViewById(R.id.tv_device_ap_add_help);
        help.setOnClickListener(this);
        button.setOnClickListener(this);
        button.setAlpha(0.4f);
        box = findViewById(R.id.cb_listener);
        box.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                button.setAlpha(1f);
            } else {
                button.setAlpha(0.4f);
            }
        });
        findViewById(R.id.back_toolbar).setOnClickListener(v -> {
            if (getContext() instanceof Activity) {
                Activity activity = (Activity) getContext();
                activity.finish();
            }
        });
    }
}
